﻿using IRUM.Utils;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace IRUM.UI
{
    public partial class FormAnalysisMain : Form
    {
        public FormAnalysisMain()
        {
            InitializeComponent();
        }

        private void FormAnalysisMain_Load(object sender, EventArgs e)
        {
            ckbKospiUp.Checked = true;
            txtKospiUp.Text = "10000";

            ckbFutureUp.Checked = true;
            txtFutureUp.Text = "5000";

            //Query();
        }

        private void btnQuery_Click(object sender, EventArgs e)
        {
            Query();
        }

        private void Query()
        {
            IRUMDataClassesDataContext dc = new IRUMDataClassesDataContext();
            IQueryable<vwITForeign> it = dc.vwITForeigns.Select(p => p);

            if (ckbKospiUp.Checked == true)
            {
                it = it.Where(p => p.KOSPI >= Convert.ToInt32(txtKospiUp.Text)).Select(p => p);
            }

            if (ckbKospiDown.Checked == true)
            {
                it = it.Where(p => p.KOSPI <= Convert.ToInt32(txtKospiDown.Text)).Select(p => p);
            }

            if (ckbKosdaqUp.Checked == true)
            {
                it = it.Where(p => p.KOSDAQ >= Convert.ToInt32(txtKosdaqUp.Text)).Select(p => p);
            }

            if (ckbKosdaqDown.Checked == true)
            {
                it = it.Where(p => p.KOSDAQ <= Convert.ToInt32(txtKosdaqDown.Text)).Select(p => p);
            }

            if (ckbFutureUp.Checked == true)
            {
                it = it.Where(p => p.Future >= Convert.ToInt32(txtFutureUp.Text)).Select(p => p);
            }

            if (ckbFutureDown.Checked == true)
            {
                it = it.Where(p => p.Future <= Convert.ToInt32(txtFutureDown.Text)).Select(p => p);
            }

            if (ckbCallUp.Checked == true)
            {
                it = it.Where(p => p.Call >= Convert.ToInt32(txtCallUp.Text)).Select(p => p);
            }

            if (ckbCallDown.Checked == true)
            {
                it = it.Where(p => p.Call <= Convert.ToInt32(txtCallDown.Text)).Select(p => p);
            }

            if (ckbPutUp.Checked == true)
            {
                it = it.Where(p => p.Put >= Convert.ToInt32(txtPutUp.Text)).Select(p => p);
            }

            if (ckbCallDown.Checked == true)
            {
                it = it.Where(p => p.Put <= Convert.ToInt32(txtPutDown.Text)).Select(p => p);
            }

            it = it.OrderByDescending(p => p.nalza);

            if (it != null)
            {
                vwITForeignDataGridView.DataSource = it;
                vwITForeignDataGridView.Rows[0].Selected = false;
                vwITForeignDataGridView.Rows[0].Selected = true;
            }
        }

        private void vwITForeignDataGridView_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex > -1)
            {
                string date = vwITForeignDataGridView.Rows[e.RowIndex].Cells["nalza"].Value as string;
                Dictionary<string, object> param = new Dictionary<string, object>();
                param.Add("date", date);
                param.Add("nCnt", 200);

                DataSet ds = DataStore.Instance.ProcedureToDataSet("sp_ChartDataDay_DailyFuture_s", param, false);

                if (ds != null)
                {
                    List<IRUM.Utils.ChartDataForDraw> list = new List<Utils.ChartDataForDraw>();
                    List<float> lstMA_05 = new List<float>();
                    List<float> lstMA_20 = new List<float>();
                    List<float> lstMA_60 = new List<float>();
                    List<float> lstMA_120 = new List<float>();

                    foreach (DataRow dr in ds.Tables[0].Rows)
                    {
                        IRUM.Utils.ChartDataForDraw draw = new ChartDataForDraw();
                        draw.date = dr["date"].ToString();
                        draw.open = Convert.ToSingle(dr["open"]);
                        draw.high = Convert.ToSingle(dr["high"]);
                        draw.low = Convert.ToSingle(dr["low"]);
                        draw.close = Convert.ToSingle(dr["close"]);
                        draw.change = dr["change"].Equals(DBNull.Value) ? 0 : Convert.ToSingle(dr["change"]);
                        draw.rate = dr["rate"].Equals(DBNull.Value) ? 0 : Convert.ToSingle(dr["rate"]);
                        draw.it = dr["foreign_sum"].Equals(DBNull.Value) ? 0 : Convert.ToInt32(dr["foreign_sum"]);

                        AddMAListData(lstMA_05, 5, draw.close);
                        AddMAListData(lstMA_20, 20, draw.close);
                        AddMAListData(lstMA_60, 60, draw.close);
                        AddMAListData(lstMA_120, 120, draw.close);


                        draw.ma_05 = lstMA_05.Count == 5 ? lstMA_05.Average() : 0;
                        draw.ma_20 = lstMA_20.Count == 20 ? lstMA_20.Average() : 0;
                        draw.ma_60 = lstMA_60.Count == 60 ? lstMA_60.Average() : 0;
                        draw.ma_120 = lstMA_120.Count == 120 ? lstMA_120.Average() : 0;

                        list.Add(draw);
                    }


                    iruM_Chart1.SetData(date, list);
                    iruM_Chart1.DrawChart();
                }
            }
        }

        private void AddMAListData(List<float> list, int length, float value)
        {
            if (list.Count == length)
            {
                list.RemoveAt(0);
                list.Add(value);
            }
            else
            {
                list.Add(value);
            }
        }

        private void vwITForeignDataGridView_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (vwITForeignDataGridView.Columns[e.ColumnIndex].HeaderText.Equals("제외") == true)
            {
                vwITForeign row = vwITForeignDataGridView.Rows[e.RowIndex].DataBoundItem as vwITForeign;
                IQueryable<vwITForeign> it = vwITForeignDataGridView.DataSource as IQueryable<vwITForeign>;

                it = it.Where(p => p.nalza.Equals(row.nalza) == false).Select(p => p);
                vwITForeignDataGridView.DataSource = it;
            }
        }
    }
}
